<script setup>
import {  Lock } from "@element-plus/icons-vue";
import { ref } from "vue";
import { ElMessage } from "element-plus";


//定义数据模型
const reSetPasswordData = ref({
  old_pwd: "",
  new_pwd: "",
  re_pwd: "",
});

//校验密码的函数
const checkRePassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("哥们 请再次确认新密码"));
  } else if (value !== reSetPasswordData.value.new_pwd) {
    callback(new Error("哥们 请确保两次输入的新密码一样"));
  } else {
    callback();
  }
};



//调用后台接口完成更改密码
import { userResetPasswordService } from "@/api/user.js";
import { useRouter } from "vue-router";
const router = useRouter();
const reSetPassword = async () => {
  //reSetPasswordData是一个响应式对象,如果要获取值,需要.value
  let result = await userResetPasswordService(reSetPasswordData.value);
    if (result.code === 0) {
    //更改密码成功
    ElMessage.success(result.msg ? result.msg : "更新密码成功了~");
    ElMessage.info('请重新登录~')
    router.push('/login')
  } else {
    ElMessage.error("更改密码失败");
    clearPasswordData();
  } 
};

//定义函数,清空数据模型的数据
const clearPasswordData = () => {
  reSetPasswordData.value = {
    old_pwd: "",
    new_pwd: "",
    re_pwd: "",
  };
};
//定义表单校验规则
const rules = {
  old_pwd: [
    { required: true, message: "哥们 请输入旧密码", trigger: "blur" },
    {
      min: 5,
      max: 16,
      message: "哥们 密码长度为5-16位非空字符",
      trigger: "blur",
    },
  ],
  new_pwd: [
    { required: true, message: "哥们 请输入新密码", trigger: "blur" },
    {
      min: 5,
      max: 16,
      message: "哥们 密码长度为5-16位非空字符",
      trigger: "blur",
    },
  ],
  re_pwd: [{ validator: checkRePassword, trigger: "blur" }],
};
</script>

<template>
  <el-row class="resetPw-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 更改密码表单 -->
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        :model="reSetPasswordData"
        :rules="rules"
      >
        <el-form-item>
          <h1>更改密码</h1>
        </el-form-item>
        <el-form-item prop="oldPassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入旧密码"
            v-model="reSetPasswordData.old_pwd"
          ></el-input>
        </el-form-item>
        <el-form-item prop="newPassword">
          <el-input 
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入新密码"
            v-model="reSetPasswordData.new_pwd"
          ></el-input>
        </el-form-item>
        <el-form-item prop="rePassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请再次输入新密码"
            v-model="reSetPasswordData.re_pwd"
          ></el-input>
        </el-form-item>
        <!-- 更改密码按钮 -->
        <el-form-item>
          <el-button
            class="button"
            type="primary"
            auto-insert-space
            @click="reSetPassword"
          >
            更改密码
          </el-button>
          
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
/* 样式 */
.resetPw-page {
  height: 100vh;
  background-color: #e7f5f4;

  .bg {
    background: url("@/assets/rePwd_bg.jpg") no-repeat center / cover;
    border-radius: 0 20px 20px 0;
    background-size: cover; /* 添加这一行 */
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
